package cmd

import (
	"fmt"
	apiv1 "gitee.com/YunFeiGuoJi/golearning/api/v1"
	"gitee.com/YunFeiGuoJi/golearning/pkg/cluster/install"
	"gitee.com/YunFeiGuoJi/golearning/pkg/util"
	//"gitee.com/YunFeiGuoJi/golearning/version"
	"github.com/spf13/cobra"
	//"time"
)

// clusterCmd represents the cluster command
var clusterCmd = &cobra.Command{
	Use:   "cluster",
	Short: "Create a Kubernetes or KubeSphere cluster",
	RunE: func(cmd *cobra.Command, args []string) error {
		logger := util.InitLogger(opt.Verbose)
		return install.CreateCluster(opt.ClusterCfgFile, opt.Kubernetes, logger, opt.Verbose, opt.SkipCheck, opt.SkipPullImages, opt.InCluster, opt.LocalStorage, opt.DownloadCmd)
	},
}

func init() {
	createCmd.AddCommand(clusterCmd)

	clusterCmd.Flags().StringVarP(&opt.ClusterCfgFile, "filename", "f", "", "Path to a configuration file")
	clusterCmd.Flags().StringVarP(&opt.Kubernetes, "with-kubernetes", "", apiv1.DefaultKubeVersion, "Specify a supported version of kubernetes")
	clusterCmd.Flags().BoolVarP(&opt.LocalStorage, "with-local-storage", "", false, "Deploy a local PV provisioner")
	clusterCmd.Flags().BoolVarP(&opt.SkipCheck, "yes", "y", false, "Skip pre-check of the installation")
	clusterCmd.Flags().BoolVarP(&opt.SkipPullImages, "skip-pull-images", "", false, "Skip pre pull images")
	clusterCmd.Flags().StringVarP(&opt.DownloadCmd, "download-cmd", "", "curl -L -o %s %s",
		`The user defined command to download the necessary binary files. The first param '%s' is output path, the second param '%s', is the URL`)

	if err := setValidArgs(clusterCmd); err != nil {
		panic(fmt.Sprintf("Got error with the completion setting"))
	}
}

//func setValidArgs(cmd *cobra.Command) (err error) {
//	cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) (
//		strings []string, directive cobra.ShellCompDirective) {
//		versionArray := []string{"v2.1.1", "v3.0.0", "v3.1.0", time.Now().Add(-time.Hour * 24).Format("nightly-20060102")}
//		return versionArray, cobra.ShellCompDirectiveNoFileComp
//	}
//
//	err = cmd.RegisterFlagCompletionFunc("with-kubernetes", func(cmd *cobra.Command, args []string, toComplete string) (
//		strings []string, directive cobra.ShellCompDirective) {
//		return version.SupportedK8sVersionList(), cobra.ShellCompDirectiveNoFileComp
//	})
//	return
//}
